package com.yangit.temp.util.pdf.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;
 
public class PdfUtil {
	
	/**
	 * 设置PDF响应头
	 *
	 * @param response
	 * @return 
	 * @author hongwei.lian
	 * @date 2018年9月10日 下午6:02:47
	 */
	public static HttpServletResponse setPdfResponse(HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/pdf");
		return response;
	}
	
	/**
	 * 打印PDF
	 *
	 * @param filepath
	 * @param outputStream 
	 * @author hongwei.lian
	 * @date 2018年9月10日 下午6:04:26
	 */
	public static void printLetter(String filepath, OutputStream outputStream){
		FileInputStream inputStream = null;
		try {
			File file = new File(filepath);
			inputStream = new FileInputStream(file);
			byte[] data = new byte[(int)file.length()];
			inputStream.read(data);
	        outputStream.write(data);
	        outputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (Objects.nonNull(outputStream)) {
					outputStream.close();
				}
				if (Objects.nonNull(inputStream)) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}